<?php

/* Copyright (c) 2007 Alec Henriksen
 * phpns is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public Licence (GPL) as published by the Free
 * Software Foundation; either version 2 of the Licence, or (at your option) any
 * later version.
 * Please see the GPL at http://www.gnu.org/copyleft/gpl.html for a complete
 * understanding of what this license means and how to abide by it.
*/

//connect to database.themes, and find selected theme

$themesql = "SELECT * FROM themes WHERE theme_selected='1'";
$themeres = mysql_query($themesql) or die ($error['query']);

//Set variables for paths
$admin = "admin.tpl.php";
$login = "login.tpl.php";
$help = "help.tpl.php";

while ($trow = mysql_fetch_assoc($themeres)) {     // get theme data
	//change file tread upon page type in global vars
	if ($globalvars['pagetype'] == "admin") {
		$file = file_get_contents($trow['theme_dir'].$admin);   //read admin file
	} elseif ($globalvars['pagetype'] == "login") {
		$file = file_get_contents($trow['theme_dir'].$login);   //read login file
	} elseif ($globalvars['pagetype'] == "help") {
		$file = file_get_contents($trow['theme_dir'].$help);   //read help file
	}
	//modification of theme here (var replacement, img fix, ect ect)
	$uri = $_SERVER['PHP_SELF'];     //fix img and other path problems
	$rep_replace = 'themes/'.$trow['base_dir'].'/';
	$uri = preg_replace($rep_patt,$rep_replace,$uri);
	$file = str_replace('{prepath}','http://'.$_SERVER['SERVER_NAME'].$uri,$file);  //fix prob
	
	//page-specific description generation
	include("inc/page_desc.php");
	if ($globalvars['pagetype'] == "admin") {  //if page is "admin" replace admin vars
		//admin configuration variables replaced with the system generated content
		$phpns_logo = '<a href="index.php"><img src="images/logo.png" alt="phpns logo" border="0" /></a>';
		$index_link = '<a href="index.php">Index</a>';
		$current_page_name = $globalvars['page_name'];
		$version = 'v2.1.1';
		$welcome_message = '';
		$time = $globalvars['time'];
		$date = $globalvars['time'];
		$notice_siteonline = $globalvars['siteonline'];
		$notice_commenabled = '';
		$notice_pending_articles = '';
		$notice_pending_articles_num = '';
		$notice_global_messages = '';
		$notice_global_messages_num = '';
		$main_content = '';
		$search_box = '';
		$logout_link = '<a href="logout.php">Logout()</a>';
		$javascript = '
<script language="javascript" type="text/javascript" src="inc/wysiwyg/tiny_mce_gzip.js"></script>
<script language="javascript" type="text/javascript">
tinyMCE_GZ.init({
	theme : "advanced",
	mode : "exact",
	elements : "main, full",

	content_css : "example_advanced.css",
	extended_valid_elements : "a[href|target|name]",
	plugins : "table",
	theme_advanced_buttons2_add : "forecolor,backcolor,seperator",
	theme_advanced_buttons3_add_before : "tablecontrols,separator",
	//invalid_elements : "a",
	theme_advanced_styles : "Header 1=header1;Header 2=header2;Header 3=header3;Table Row=tableRow1", // Theme specific setting CSS classes
	//execcommand_callback : "myCustomExecCommandHandler",
	debug : false
});
</script>
<script language="javascript" type="text/javascript">
tinyMCE.init({
	theme : "advanced",
	mode : "exact",
	elements : "main",
	content_css : "example_advanced.css",
	extended_valid_elements : "a[href|target|name]",
	plugins : "table",
	theme_advanced_buttons1_add_before : "forecolor,backcolor,separator",
	theme_advanced_buttons3_add_before : "tablecontrols,separator",
	//invalid_elements : "a",
	//execcommand_callback : "myCustomExecCommandHandler",
	debug : false
});
		
function togglewysiwyg(id) {
	var elm = document.getElementById(id);
	if (tinyMCE.getInstanceById(id) == null)
		tinyMCE.execCommand(\'mceAddControl\', false, id);
	else
		tinyMCE.execCommand(\'mceRemoveControl\', false, id);
	}

function expandwysiwyg(id) {
	var expelm = document.getElementById(id);
	var expstyle = expelm.style.height;
	document.getElementById(id).style.height = "500px";
	togglewysiwyg(id);
	togglewysiwyg(id);
}
		
function validate_required(field,alerttxt) {
	with (field) {
		if (value==null||value=="") {
			alert(alerttxt);
			return false
		} else {
			return true
		}
	}
}
function validate_form(thisform) {
	with (thisform) {
		if (validate_required(title,"You must enter a title to continue.")==false) {
			return false
		}
	}
}
function Checkall(form){ 
	for (var i = 1; i < form.elements.length; i++) {    
		eval("form.elements[" + i + "].checked = form.elements[0].checked");  
	} 
}

function new_window(url)
{
	var newwindow;
	newwindow=window.open(url,\'name\',\'height=500,width=400,left=100,top=100,resizable=yes,scrollbars=yes,status=yes\');
	if (window.focus) {newwindow.focus()}
}
</script>
		';	
		
		//set the page image
		$page_image = '<img src="images/'. $globalvars['page_name'] . '.png" alt="'. $globalvars['page_name'] .'" alt="'.$globalvars['page_name'].' icon" title="'.$globalvars['page_name'].' icon" class="icon" />';
			//however, if there is a specific page image specified
			if ($globalvars['page_image']) {
				$page_image = '<img src="images/'. $globalvars['page_image'] . '.png" alt="'. $globalvars['page_name'] .'" alt="'.$globalvars['page_name'].' icon" title="'.$globalvars['page_name'].' icon" class="icon" />';
			} elseif ($globalvars['page_image'] == "none") {
				$page_image = '';
			}


		$file = str_replace('{phpns_logo}',$phpns_logo,$file);
		$file = str_replace('{version}',$globalvars['version'],$file);
		$file = str_replace('{username}',$_SESSION['username'],$file);
		$file = str_replace('{javascript}',$javascript,$file);
		$file = str_replace('{page_image}',$page_image,$file);
		$file = str_replace('{index_link}',$index_link,$file);
		$file = str_replace('{content}',$content,$file);
		$file = str_replace('{page_desc}',$page_desc[$globalvars['page_name']],$file);
		$file = str_replace('{current_page_name}',$current_page_name,$file);
		$file = str_replace('{version}',$version,$file);
		$file = str_replace('{welcome_message}',$welcome_message,$file);
		$file = str_replace('{admin_notes}',$globalvars['admin_notes'],$file);
		$file = str_replace('{server_time}',$time,$file);
		$file = str_replace('{server_date}',$date,$file);
		$file = str_replace('{notice_siteonline}',$notice_siteonline,$file);
		$file = str_replace('{notice_comments_enabled}',$notice_comments_enabled,$file);
		$file = str_replace('{notice_pending_articles}',$notice_pending_articles,$file);
		$file = str_replace('{notice_pending_articles_num}',$notice_pending_articles_num,$file);
		$file = str_replace('{notice_global_messages}',$notice_global_messages,$file);
		$file = str_replace('{notice_global_messages_num}',$notice_global_messages_num,$file);
		$file = str_replace('{main_content}',$main_content,$file);
		$file = str_replace('{search_box}',$search_box,$file);
		$file = str_replace('{logout_link}',$logout_link,$file);
		
		$global_message = load_config('global_message'); //load config values for global message
		if ($global_message['v3'] != NULL) { //if the global message is non-existant (null)
			$global_message['v3'] = decode_data($global_message['v3']);
			$file = str_replace('{global_message}','<div class="global_message"><div class="global_message_link"><a href="preferences.php?do=globalmessage">[Change Message]</a></div>'.$global_message['v3'].'</div>',$file); //replace login message
		} else {
			$file = str_replace('{global_message}','',$file);
		}
		

	} elseif ($globalvars['pagetype'] == "login") {
			//grab db content
			$global_message = load_config('global_message');
			
					$file = str_replace('{content}',$error_message.$content,$file);
		$file = str_replace('{logo}',$error_message.'<a href="index.php"><img src="images/logo.png" id="logo" class="logo" /></a>',$file);
			if ($global_message['v3'] != NULL) { //if the global message is non-existant (null)
				$global_message['v3'] = decode_data($global_message['v3']);
				$file = str_replace('{global_message}','<div class="global_message">'.$global_message['v3'].'</div>',$file); //replace login message
			} else {
				$file = str_replace('{global_message}','',$file);
			}
		$file = str_replace('{version}',$globalvars['version'],$file);
		$file = str_replace('{current_page_name}',$current_page_name,$file);
		
	} elseif ($globalvars['pagetype'] == "help") {
	}
	//end modification of theme


	echo $file;  //output theme, decoded, and vars replaced.
}


?>
